package com.zzw.service;

import com.zzw.entity.OauthAccessToken;
import com.zzw.mapper.dao.OauthAccessTokenMapper;
import com.zzw.util.MyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "oauthAccessTokenService")
public class OauthAccessTokenService{
    @Autowired
    protected OauthAccessTokenMapper mapper;

    @CachePut(key = "#p0.id", unless = "#result eq null")  //#p0表示第一个参数
    public OauthAccessToken insert(OauthAccessToken oauthAccessToken) {
        mapper.insert(oauthAccessToken);
        return oauthAccessToken;
    }

    @CachePut(key = "#p0.id", unless = "#result eq null")  //#p0表示第一个参数
    public OauthAccessToken update(OauthAccessToken oauthAccessToken) {
        mapper.updateByPrimaryKey(oauthAccessToken);
        return oauthAccessToken;
    }

    @CacheEvict(key = "#p0.id")  //删除缓存名称为userCache,key等于指定的id对应的缓存
    public int delete(OauthAccessToken oauthAccessToken) {
        return mapper.delete(oauthAccessToken);
    }

    @Cacheable(key = "#p0.id", unless = "#result eq null")  //#p0表示第一个参数
    public OauthAccessToken select(OauthAccessToken oauthAccessToken) {
        return mapper.selectOne(oauthAccessToken);
    }

    @Cacheable(key = "#p0", unless = "#result eq null")  //#p0表示第一个参数
    public OauthAccessToken findByTokenId(String tokenId) {
        return mapper.findByTokenId(tokenId);
    }

    @CacheEvict(key = "#p0")  //删除缓存名称为userCache,key等于指定的id对应的缓存
    public int deleteById(String tokenId) {
        return mapper.deleteByTokenId(tokenId);
    }
    public int deleteByRefreshToken(String refreshToken){
          return  mapper.deleteByRefreshToken(refreshToken);
    }
    public List<OauthAccessToken> findByNameAndClientId(String userName, String clientId) {
        return mapper.findByNameAndClientId(userName,clientId);
    }
    public List<OauthAccessToken> findByClientId( String clientId) {
        return mapper.findByClientId(clientId);
    }
    @CacheEvict(allEntries=true)
    public int deleteAll() {
        return mapper.delete(null);
    }

    @CacheEvict(allEntries=true)
    public int deleteByIds(String ids) {
        return mapper.deleteByIds(ids);
    }
}
